package com.goods.business.service.imp;

import com.goods.business.mapper.ProductMapper;
import com.goods.business.service.ProductService;
import com.goods.common.vo.business.ProductVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.validation.constraints.NotNull;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
@ComponentScan(value = "com.goods.business.mappper")
public class ProductServiceImol implements ProductService {
    @Autowired
    ProductMapper productMapper;


    @Override
    public void updateProductById(Integer id, ProductVO productVO) {
        productVO.setModifiedTime(new Date());
        productVO.setId(id.longValue());
        productVO.setStatus(2);
        productMapper.updateByPrimaryKey(productVO);
    }

    @Override
    public void productAdd(ProductVO productVO) {
        productVO.setCreateTime(new Date());
        //TODO
        @NotNull(message = "分类不能为空") Long[] categoryKeys = productVO.getCategoryKeys();
        productVO.setOneCategoryId(categoryKeys[0]);
        productVO.setTwoCategoryId(categoryKeys[1]);
        productVO.setStatus(2);
        productMapper.insert(productVO);
    }

    @Override
    public List<ProductVO> findProductList(Integer pageNum, Integer pageSize, String name, Integer categoryId, Integer status) {
        if (!StringUtils.isEmpty(status)){
            ArrayList<ProductVO> productVOArrayList = new ArrayList<ProductVO>();
            List<ProductVO> productVOS = productMapper.selectAll();
            for (ProductVO productVO : productVOS) {
                if (status==productVO.getStatus()){
                    productVOArrayList.add(productVO);
                }
            }
            return productVOArrayList;
        }
        List<ProductVO> productVOS = productMapper.selectAll();

        return productVOS;
    }
}
